﻿/*  
 * 作者: Elan
 * 创建时间: 2013-5-2 20:49:36  
 
 * 说明:   玲珑数据信息系统主窗体
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using DevComponents.DotNetBar;
using Fasterflect;
using LL.Business;
using LL.Common;
using LL.DevComponents;
using LL.Utility.WindowsForms;
using Microsoft.Practices.Unity;
using NanoERP.Business.Permission;
using NanoERP.Infrastructure;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;

namespace NanoERP
{
    partial class MainForm
    {
        private IUnityContainer _moduleContainer;
        private IUnityContainer ModuleContainer
        {
            get { return _moduleContainer ?? (_moduleContainer = new UnityContainer()); }
            set { _moduleContainer = value; }
        }

        private void Initlize()
        {
            ModuleTree tree = null;
            using (var session = BusinessSessionFactory.NewSession())
            {
                tree = session.Get<ModuleService>()
                    .GetModuleTreeByUser(LogonSession.LogonUser.Id);
            }
            InitlizeModules(tree);
            InitMenus(tree);
        }

        private void InitlizeModules(ModuleTree tree)
        {
            var types = Assembly.GetExecutingAssembly()
                .TypesImplementing<ModuleForm>();
            foreach (var category in tree.Children)
            {
                foreach (var module in category.Modules)
                {
                    var type1 = types.Where(d => ModuleAttribute
                        .GetModuleNames(d).Contains(module.Code))
                        .FirstOrDefault();

                    if (type1 == null)
                        continue;
                    ModuleContainer.RegisterType(typeof(ModuleForm), type1, module.Code);
                }
            }
        }

        private void InitMenus(ModuleTree tree)
        {
            var resMgr = new ComponentResourceManager(this.GetType());
            sideBar1.Panels.Clear();
            var baseItem = new ButtonItem()
            {
                ImagePosition = eImagePosition.Top,
                ButtonStyle = eButtonStyle.ImageAndText,
                //默认图标
                Image = Properties.Resources.btnAdjustment_Image,
            };
            foreach (var category in tree.Children)
            {
                if (!sideBar1.ContainsPanel(category.Name))
                {
                    var img = resMgr.GetObject(category.ImageKey ?? "").As<Image>();
                    var panel = new SideBarPanelItem(category.Name, category.Name);
                    if (img != null)
                        panel.Image = img;
                    sideBar1.Panels.Add(panel);
                }
                foreach (var module in category.Modules)
                {
                    var item = baseItem.Copy() as ButtonItem;
                    item.Text = module.Name;
                    var img = resMgr.GetObject(module.ImageKey ?? "") as Image;
                    if (img != null)
                        item.Image = img;
                    var module1 = module;
                    item.Command = new DevComponents.DotNetBar.Command(
                        (x, y) => Workbench.CurrentWorkspace.LoadModule(module1.Code, null));
                    sideBar1.Panels[category.Name].SubItems.Add(item);
                }
            }
        }

    }
}
